import { defineStore } from 'pinia'

const messageStore = defineStore('message', {
    // { message: '', type: '', duration: 0, showClose: false, show: false }
    state: () => ({ messages: [] }),
    actions: {
        hide(index) {
            this.messages = this.messages.filter((item, i) => i !== index)
        },
        Message(obj) {
            let type = obj.type || 'success'
            let message = obj.message
            let duration = obj.duration || 2500
            let showClose = obj.showClose || false
            let show = true
            this.messages.push({
                message,
                type,
                duration,
                showClose,
                show
            })
        },
        success(message, duration, showClose) {
            this.Message({
                type: "success",
                message: message,
                duration: duration,
                showClose: showClose
            })
        },
        error(message, duration, showClose) {
            this.Message({
                type: "error",
                message: message,
                duration: duration,
                showClose: showClose
            })
        },
        warning(message, duration, showClose) {
            this.Message({
                type: "warning",
                message: message,
                duration: duration,
                showClose: showClose
            })
        },
        info(message, duration, showClose) {
            this.Message({
                type: "info",
                message: message,
                duration: duration,
                showClose: showClose
            })
        },

    },
})

export default messageStore